<?php
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/*
 * GEM/Model/Mapper/UserMapper.php
 * 
 * 
 * @author David Joly
 * @link http://zeletron.com
 * 
 * @category GEM
 * @package GEM_Model
 * @subpackage GEM_Model_Mapper
 */
class GEM_Model_Mapper_UserMapper extends GEM_Model_Mapper_Mapper
{
	/**
	 * Prepared Insert Statement
	 * 
	 * @var PDOStatement
	 */
	protected $_insertStm;
		
	/**
	 * Constructor
	 * 
	 * @return void
	 */
	public function __construct(){
		parent::__construct();
		
		//Initialize Prepared Statements
		$this->_initStatements();
	}
	
	/**
	 * Initialize Prepared Statements
	 * 
	 * @return void
	 */
	protected function _initStatements(){
		$this->_insertStm = $this->_db->prepare(
			"INSERT INTO user SET 
				username = :username, 
				fullname = :fullname, 
				email = :email,
				password = :password,
				createdDate = :createdDate,
				lastAccess = :lastAccess,
				lastLogin = :lastLogin,
				timezone = :timezone,
				locale = :locale"	
		);
	}
	
	/**
	 * Used by parent class to dynamically build common prepared
	 * statements.
	 * @see GEM_Model_Mapper_Mapper#_getTable()
	 * 
	 * @return string
	 */
	protected function _getTable(){
		return 'user';
	}
	
	/**
	 * Used by parent class to dynamically locate collection.
	 * @see GEM_Model_Mapper_Mapper#getCollection()
	 * 
	 * @return string
	 */
	protected function _getClassType(){
		return 'User';
	}
	
	/**
	 * @param array $fields
	 * @return GEM_Model_Domain_User
	 */
	protected function _doCreateObject(array $fields){
		$user = new GEM_Model_Domain_User($fields);
		return $user;
	}
	
	/**
	 * Inserts a new user to the database.
	 * @param GEM_Model_Domain_User $obj
	 * @return void
	 */
	protected function _doInsert(GEM_Model_Domain_Object $obj){
		if(!($obj instanceof GEM_Model_Domain_User))
			throw new GEM_Model_Domain_Exception("Object type GEM_Model_Domain_User Expected.");
		$this->_insertStm->execute(array(
			':username'	=> $obj->getUsername(),
			':fullname'	=> $obj->getFullName(),
			':email'	=> $obj->getEmail(),
			':password' => $obj->getPassword(),
			':createdDate'	=> date('Y-m-d H:i:s'),
			':lastAccess'	=> date('Y-m-d H:i:s'),
			':lastLogin'	=> date('Y-m-d H:i:s'),
			':timezone'		=> $obj->getTimezone(),
			':locale'		=> $obj->getLocale()
		));
		$obj->setId($this->_db->lastInsertId());
	}
	/**
	 * @param GEM_Model_Domain_User $obj
	 * @return void
	 */
	protected function _doDelete(GEM_Model_Domain_Object $obj){
		if(!($obj instanceof GEM_Model_Domain_User))
			throw new GEM_Model_Domain_Exception("Object type GEM_Model_Domain_User_User Expected.");
		$this->_deleteStm->execute(array($obj->getId));
	} 
	
	/**
	 * Searches for a user by email.
	 * 
	 * @param string $email
	 * @return GEM_Model_Domain_User
	 */
	public function findByEmail($email){
		$existingObj = GEM_Model_Domain_ObjectManager::deepSearch('GEM_Model_Domain_User',array('email' => $email));
		if($existingObj)
			return $existingObj;
		$stm = $this->_db->prepare("SELECT * FROM user WHERE email = :email");
		$stm->execute(array(':email' => $email));
		$fields = $stm->fetch();
		$stm->closeCursor();
		if(!is_array($fields))
			return null;
		$obj = $this->_doCreateObject($fields);
		return $obj;
	}
	
	/**
	 * Searches for a user by username.
	 * 
	 * @param string $username
	 * @return GEM_Model_Domain_User
	 */
	public function findByUsername($username){
		$existingObj = GEM_Model_Domain_ObjectManager::deepSearch('GEM_Model_Domain_User',array('username' => $username));
		if($existingObj)
			return $existingObj;
		$stm = $this->_db->prepare("SELECT * FROM user WHERE username = :username");
		$stm->execute(array(':username' => $username));
		$fields = $stm->fetch();
		$stm->closeCursor();
		if(!is_array($fields))
			return null;
		$obj = $this->_doCreateObject($fields);
		return $obj;
	}
	
}